﻿
@{
    ViewBag.title = "医嘱审核";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.js")"></script>
<link href="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.css")" rel="stylesheet" />
<style>
    .dv-left {
        width: 200px;
        float: left;
    }

    .dv-left-tree {
        overflow-y: auto;
        border-radius:4px;
        background:white;
    }

    .dv-left-title {
        padding: 6px 10px;
        border-bottom: 1px solid transparent;
        border-top-left-radius: 3px;
        border-top-right-radius: 3px;
        border-color:#ddd;
        background-color: white;
    }

    /*.dv-left-title {
        width: 100%;
        height: 30px;
        line-height: 30px;
        background-color: #00a0e9;
        text-align: center;
        color: white;
    }*/

    .dv-right {
        float: left;
        margin-left: 5px;
    }

    .dv-right-order {
        width: 100%;
    }

    .dv-grid {
        width: 100%;
    }
</style>


<div class="dv-body">
    <div class="dv-left">
        <div class="dv-left-title">患者列表</div>
        <div class="dv-left-tree" id="BqTreeList"></div>
    </div>
    <div class="dv-right">
        <div class="dv-right-order">
            <div class="topPanel">
                <div class="toolbar">
                    <div class="btn-group">
                        <a class="btn btn-primary" onclick="RefreshOrder()"><span class="fa fa-refresh"></span></a>
                    </div>
                    <div class="btn-group">
                        <a id="AuditChecked" class="btn btn-primary" onclick="AuditChecked()">
                            <i class="fa fa-pencil-square-o"></i>审核当前
                        </a>
                    </div>
                    <div class="btn-group">
                        <a id="AuditTemporary" class="btn btn-primary" onclick="AuditOrders(1)">
                            <i class="fa fa-pencil-square-o"></i>审核临时
                        </a>
                    </div>
                    <div class="btn-group">
                        <a id="AuditLong" class="btn btn-primary" onclick="AuditOrders(2)">
                            <i class="fa fa-pencil-square-o"></i>审核长期
                        </a>
                    </div>
                    <div class="btn-group">
                        <a id="AuditAll" class="btn btn-primary" onclick="AuditOrders(0)">
                            <i class="fa fa-pencil-square-o"></i>审核全部
                        </a>
                    </div>
                </div>
            </div>
            <div class="gridPanel">
                <table id="gridList" ></table>
                <div id="gridPager"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(".dv-body").height($(window).height());
    $(".dv-body").width($(window).width());
    var rightWidth = $(".Newtouch_iframe", parent.document).width() - $(".dv-left").width() - 25;
    $(".dv-right").width(rightWidth);
    $(".dv-left-tree").height($(window).height()-70);

    $(function () {
        $(window).resize(function () {//浏览器窗口调整大小时重新计算宽高
            $(".dv-body").height($(window).height());
            $(".dv-body").width($(window).width());
        });
        $('#gridList').jqGrid("clearGridData");
        //$('#gridList').newtouchLocalDataGrid(null, orderList);
        getBqTreeList();
        gridList("");
    });
 
    function getBqTreeList()
    {       
        $("#BqTreeList").treeview({
            height: 400,
            slimscroll: false,
            showcheck: true,
            url: "/NurseManage/OrderAudit/GetPatWardTree",
            //param: { aa: "" },
            oncheckboxclick: function (item, s) {           
                getOrderAuditList(item,s);
            }
        });

    }
    
    function getOrderAuditList(item,s)
    {
        var patlist = $("#BqTreeList").getCheckedNodeObjArray();
        var patlistFath = "";

        var patzyh = "";
        var patzyhItem = "";

        //当前选中父节点
        var len = 0;
        var single = 0; //是否单个子节点

        if (item != null && item.Ex1 == "p") {
            patlistFath = item.ChildNodes;
            len = patlistFath.length;
        }
        else if (item != null && item.Ex1 == "c")
        {
            patlistFath = item;
            len = 1;
            single = 1;
        }

        //选中节点
        for (var i = 0; i < patlist.length; i++) {
            if (patlist[i].Ex1 == "c") {
                patzyh += patlist[i].value + ",";
            }
        }

        if (s == 1)
        {
            if (single == 0) {
                for (var j = 0; j < len; j++) {
                    if (patlistFath[j].Ex1 == "c") {
                        patzyh += patlistFath[j].value + ",";
                    }
                }
            }
            else if (patlistFath.Ex1 == "c") {
                patzyh += patlistFath.value ;
            }
        }
        else if (s == 0)
        {
            if (single == 0)
            {
                for (var j = 0; j < len; j++) {
                    if (patlistFath[j].Ex1 == "c" && patlistFath[j].checkstate == 1) {
                        patzyh = patzyh.replace(patlistFath[j].value + ",", "");
                    }
                }
            }
            else if (patlistFath.Ex1 == "c") {
                patzyh = patzyh.replace(patlistFath.value + ",", "");
            }

        }

        //$('#gridList').jqGrid("clearGridData");
        $('#gridList').jqGrid('setGridParam', {
            postData: {
                patList: patzyh
            }
        }).trigger("reloadGrid");
    }

    function getOrderListByPatId(patid)
    {
        $('#gridList').jqGrid("clearGridData");
        $('#gridList').newtouchLocalDataGrid(null, orderList);
    }

    //级联选中所有子节点
    function checkAllSon(node) {
        $('#BqTreeList').treeview('checkNode', node.nodeId, { silent: true });
        if (node.nodes != null && node.nodes.length > 0) {
            for (var i in node.nodes) {
                checkAllSon(node.nodes[i]);
            }
        }
    }
    //级联取消所有子节点
    function uncheckAllSon(node) {
        $('#BqTreeList').treeview('uncheckNode', node.nodeId, { silent: true });
        if (node.nodes != null && node.nodes.length > 0) {
            for (var i in node.nodes) {
                uncheckAllSon(node.nodes[i]);
            }
        }
    }

    var checkTrigger = false;
    var list = new Array();
    var liststu = false;
    var j = 0;

    function gridList(patzyh) {
        
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            postData: { patList: patzyh },
            url: "/NurseManage/OrderAudit/GetGridJson",
            height: $(window).height()-130,
            colModel: [
                { label: '医嘱ID', name: 'yzid', align: 'center', width: 50, hidden: true,key:true },
                { label: '医嘱项目', name: 'xmmc', align: 'center', width: 150 },
                { label: '医嘱性质', name: 'yzxzsm', align: 'center', width: 50 },
                { label: '开始时间', name: 'kssj', align: 'center', width: 120, formatter: function (val) { return $.getTime({ date: val }); } },
                {
                    label: '医嘱内容', name: 'yznr', align: 'center', width: 300, formatter: function (val) {
                        if (val != null)
                        {
                            if (val.indexOf("[停]") != -1) {
                                return "<span style='color:red'>[停]</span>" + val.replace("[停]", "")
                            }
                            else
                                return val;
                        }

                        else return "";
                    }
                },
                { label: '停止/作废时间', name: 'tzsj', align: 'center', width: 120, formatter: function (val) { if (val != null) return $.getTime({ date: val }); else return ""; } },
                { label: '录入者', name: 'Creator', align: 'center', width: 50 },
                { label: '医嘱性质编码', name: 'yzxz', align: 'center', hidden: true },
                { label: '医嘱组号', name: 'zh', align: 'center', width: 70, formatter: function (val) { if (val != null) return val; else return ""; } }
            ],
            pager: "#gridPager",
            sortname: 'zh desc',
            viewrecords: true,
            multiselect: true,
            onSelectRow: function (rowIndex, s) {
                if (!checkTrigger) {
                    var len = 0;
                    var rowData = $("#gridList").jqGrid('getRowData', rowIndex);
                    var ids = $("#gridList").jqGrid('getRowData_AllLine');

                    liststu = s; //同步状态
                    for (var i = 0; i < ids.length; i++) {
                        if (ids[i].zh == rowData.zh && ids[i].zh != "") {
                            if (s == true) {
                                if (rowIndex != ids[i].yzid) {
                                    list[len] = ids[i].yzid;
                                    len += 1;
                                }
                            }
                            else {
                                if (rowIndex != ids[i].yzid) {
                                    list[len] = ids[i].yzid;
                                    len += 1;
                                }
                            }
                        }
                    }
                    checkTrigger = true;
                }

                if (list.length > 0) {

                    for (j = j; j < list.length; j++) {
                        if (liststu) //同组选中
                        {
                            j = j + 1;
                            $("#gridList").jqGrid("setSelection", list[j - 1], true);

                        }
                        else { //同组取消选中
                            //j = j + 1;
                            $("#gridList").jqGrid("setSelection", list[j], false);      //取消不触发onselectrow                       
                        }

                    }
                }

                j = 0;
                checkTrigger = false;
                list = new Array();
                liststu = false;
            }
        });
    }

    //刷新医嘱
    function RefreshOrder() {
        $('#gridList').jqGrid("clearGridData");
        getOrderAuditList(null, null)
    }

    //审核当前
    function AuditChecked() {
        var strYZID = [];
        var selRowIds = jQuery("#gridList").jqGrid("getGridParam", "selarrrow");
        if (selRowIds == null || selRowIds.length == 0) {
            $.modalAlert("请先选中需审核的医嘱", 'warning');
            return;
        }
        for (var i = 0; i < selRowIds.length; i++) {
            var rowData = $("#gridList").jqGrid('getRowData', selRowIds[i]);
            strYZID.push({ yzid: rowData.yzid, yzxz: rowData.yzxz });
        }

        $.ajax({
            type: "POST",
            url: "/NurseManage/OrderAudit/submitOrderList",
            data: { orderList: strYZID },
            dataType: "json",
            async: false,
            success: function (ajaxresp) {
                if (ajaxresp.state == "error" || ajaxresp.state == "") {
                    setTimeout(function () {
                        $.modalMsg(ajaxresp.message, 'error');
                    }, 1000);

                }
                else {
                    setTimeout(function () {
                        $.modalMsg('审核成功', 'success');
                    }, 1000);

                }
                getOrderAuditList(null, null);
                //getBqTreeList();
            }
        });
    }
    //审核临时  长期  全部
    function AuditOrders(yzxz) {
        var selPatTrees = $("#BqTreeList").getCheckedNodeObjArray();

        //var selPatTrees = $('#BqTreeList').treeview('getChecked');
        if (selPatTrees == null || selPatTrees.length < 1) {
            $.modalAlert("请先选中需审核的患者", 'warning');
            return;
        }
        var patzyh = "";
        for (var i = 0; i < selPatTrees.length; i++) {
            if (selPatTrees[i].Ex1 == "c")
            {
                patzyh += selPatTrees[i].value + ",";
            }
        }

        if (patzyh == "")
        {
            $.modalAlert("请先选中需审核的患者", 'warning');
            return;
        }
        $.ajax({
            type: "POST",
            url: "/NurseManage/OrderAudit/submitOrderListbyPat",
            data: { patList: patzyh, yzxz: yzxz },
            dataType: "json",
            async: false,
            success: function (ajaxresp) {
                if (ajaxresp.state == "error" || ajaxresp.state == "") {
                    setTimeout(function () {
                        $.modalMsg(ajaxresp.message, 'error');
                    }, 1000);

                }
                else {
                    setTimeout(function () {
                        $.modalMsg('审核成功', 'success');
                    }, 1000);

                }
                getOrderAuditList(null, null);
               // getBqTreeList();
            }
        });
    }

</script>
